package Servlet;

import Pojo.UserMsgpojo;
import Service.Serviceimpl.Manageserviceimpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

//获取用户信息表数据
public class ManageaddUserMsgpojoservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setContentType("text/http;charset=UTF-8");
        List<UserMsgpojo> userMsgpojo = new Manageserviceimpl().getUserMsgpojo();
        Map<String, Object> mp =new HashMap<String,Object>();
        if (userMsgpojo.size()>0){
            mp.put("msg","获取消息成功");
            mp.put("count",userMsgpojo.size());
            mp.put("code",0);
            mp.put("data",userMsgpojo);
            Gson gson = new Gson();
            String s = gson.toJson(mp);
            resp.getWriter().write(s);
        }
    }
//修改用户信息表
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String checkstatus = req.getParameter("ma");
        if (Objects.equals(checkstatus, "0")){
            checkstatus="绿码";
        }else if (Objects.equals(checkstatus, "1")){
            checkstatus="黄码";
        }else {
            checkstatus="红码";
        }
        String aSwitch = req.getParameter("switch");
        String sex = req.getParameter("sex");
        aSwitch=Objects.equals(req.getParameter("switch"), "on")? "已接种": "未接种";
        System.out.println(id+name+age+checkstatus+aSwitch+sex);
        UserMsgpojo userMsgpojo = new UserMsgpojo(id, name, age, checkstatus, aSwitch, sex);
        boolean updateusermsg = new Manageserviceimpl().updateusermsg(userMsgpojo);
        if (updateusermsg){
            resp.getWriter().write("ok");
        }else {
            resp.getWriter().write("error");
        }
    }
}
